﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class Buttons
    {
        public Buttons()
        {
            int testCase = Int32.Parse(Console.ReadLine());
            while (testCase-- > 0)
            {
                int size = Int32.Parse(Console.ReadLine());
                bool[,] initGrid = new bool[size, size];
                bool[,] xorGrid = new bool[size, size];
                bool[] xButton = new bool[size];
                bool[] yButton = new bool[size];
                //input
                for (int y = 0; y < size; y++)
                {
                    string[] consoleInput = Console.ReadLine().Split(' ');
                    for (int x = 0; x < size; x++)
                    {
                        initGrid[x, y] = consoleInput[x].Trim() == "1";
                    }
                }
                bool result = true;
                xButton[0] = false;
                for (int y = 0; y < size; y++)
                {
                    string[] consoleInput = Console.ReadLine().Split(' ');
                    if (result)
                    {
                        for (int x = 0; x < size; x++)
                        {
                            bool final = consoleInput[x].Trim() == "1";
                            xorGrid[x, y] = initGrid[x, y] ^ final;
                            if (x == 0)
                            {
                                yButton[y] = xorGrid[0, y];
                            }
                            else if (y == 0)
                            {
                                xButton[x] = xorGrid[x, 0] ^ yButton[0];
                            }
                            else if ((xButton[x] ^ yButton[y]) != xorGrid[x, y])
                            {
                                result = false;
                            }
                        }
                    }
                }
                if (!result)
                {
                    Console.WriteLine("-1");
                }
                else
                {
                    int xCount = 0;
                    int yCount = 0;
                    for (int i = 0; i < size; i++)
                    {
                        if (xButton[i])
                        { xCount++; }
                        if (yButton[i])
                        { yCount++; }
                    }
                    bool reverse = xCount + yCount > size;
                    xCount = reverse ? (size - xCount) : xCount;
                    yCount = reverse ? (size - yCount) : yCount;
                    if (xCount == 0 && yCount == 0)
                    {
                        Console.WriteLine("0");
                        continue;
                    }
                    Console.WriteLine(yCount);
                    for (int i = 0; i < size; i++)
                    {
                        if (reverse ^ yButton[i])
                        { Console.WriteLine(i); }
                    }
                    Console.WriteLine(xCount);
                    for (int i = 0; i < size; i++)
                    {
                        if (reverse ^ xButton[i])
                        { Console.WriteLine(i); }
                    }

                }
            }
        }
    }
}
